//
//  RecommendCollectionViewCell.m
//  WWAVPlayer
//
//  Created by dllo on 15/10/29.
//  Copyright © 2015年 LZY. All rights reserved.
//

#import "RecommendCollectionViewCell.h"
#import "RecommendScrollerTableViewCell.h"
#import "VerticalTableViewCell.h"
#import "HorizontalTableViewCell.h"
#import "Blocks.h"
#import "Contents.h"
@interface RecommendCollectionViewCell()<UITableViewDataSource,UITableViewDelegate,HorizontalTableViewCellDelegate,VerticalTableViewCellDelegate,RecommendScrollerTableViewCellDelegate>


@end
@implementation RecommendCollectionViewCell

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

- (void)passToCollectionWithIdNum:(NSString *)idNum
{
    [self.delegate passToMainControllerWithIdNum:idNum];
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64 + 40,Screen_Width, Screen_Height )style:UITableViewStylePlain];
        
        self.tableView.delegate = self;
        self.tableView.dataSource = self;
        
        [self.tableView registerClass:[RecommendScrollerTableViewCell class] forCellReuseIdentifier:@"scroller"];
        [self.tableView registerClass:[VerticalTableViewCell class] forCellReuseIdentifier:@"ver"];
        [self.tableView registerClass:[HorizontalTableViewCell class] forCellReuseIdentifier:@"hor"];
        
        [self.contentView addSubview:self.tableView];
    }
    return self;
}
- (void)applyLayoutAttributes:(UICollectionViewLayoutAttributes *)layoutAttributes
{
    self.tableView.frame = CGRectMake(0, 0 ,Screen_Width,layoutAttributes.bounds.size.height );
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        return Screen_Height / 3;
    }else {
        Blocks *block = [self.dataArr objectAtIndex:indexPath.section];
        if ([block.template isEqualToString:@"still"])
        {
            if (block.contents.count % 2 == 0) {
                return 125 * block.contents.count / 2;
            }else{
                return 125 *( block.contents.count / 2 );
            }
        }else{
            return 165 * block.contents.count / 3;
        }
    }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

    return 1;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.dataArr.count ;
}
//- (nullable NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
//{
//    NSString *str = @"asd";
//    return str;
//}
- (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    
    Blocks *block = [self.dataArr objectAtIndex:section];
    if (section == 0) {
        return nil;
    }else{
        UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, Screen_Width, 30 * H)];
        
        UILabel *headerLabel = [[UILabel alloc] initWithFrame:CGRectMake(10 * W, 5 * H, Screen_Width / 2 - 10 * W, 20 * H)];
    
        headerLabel.text = block.name;
        
        headerLabel.font = [UIFont systemFontOfSize:15];
        
        [headerView addSubview:headerLabel];
        
        UIView *headerButtomView = [[UIView alloc] initWithFrame:CGRectMake(10,headerView.bounds.size.height - 1 * H, Screen_Width - 20, 1 * H)];
        
        headerButtomView.backgroundColor = [UIColor grayColor];
        
        [headerView addSubview:headerButtomView];
        
        if (![[block.channel objectForKey:@"id"] isEqualToString:@""]) {
    
            UIButton *headerButton = [UIButton buttonWithType:UIButtonTypeSystem];
    
            [headerButton setTitle:@"显示更多" forState:UIControlStateNormal];
    
            [headerButton addTarget:self action:@selector(headerButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    
            headerButton.frame = CGRectMake(Screen_Width - 80 * W, 0, 80 * W, 30 * H);
        
            [headerView addSubview:headerButton];
        }
        return headerView;
    }

}
- (void)headerButtonAction:(UIButton *)btn
{
    
}

- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForHeaderInSection:(NSInteger)section NS_AVAILABLE_IOS(7_0)
{
    if(section == 0)
    {
        return 0;
    }else{
       return 30 * H;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    Blocks *block = [self.dataArr objectAtIndex:indexPath.section];
    
    if (indexPath.section == 0) {
        RecommendScrollerTableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:@"scroller"];
        
        cell.titleArr = [NSMutableArray array];
        cell.imageArr = [NSMutableArray array];
        for (NSDictionary *dic in block.contents) {
            
            NSString *titleStr = [dic objectForKey:@"name"];
            NSString *imageStr = [dic objectForKey:block.template];
            
            [cell.titleArr  addObject:titleStr];
            [cell.imageArr addObject:imageStr];
       
        }
        cell.block = block;
        cell.delegate = self;
        return cell;
    }else {
        if ([block.template isEqualToString:@"still"]) {
            HorizontalTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"hor"];
            
            cell.block = block;
            cell.delegate = self;
            [cell.collectionView reloadData];
            return cell;
        }
        else{
            VerticalTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ver"];
        
            cell.block = block;
            cell.delegate = self;
            [cell.collectionView reloadData];
            return cell;
        }
    }
}






@end
